<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-WeChatMessageResponse-editForm" id="LAY-app-WeChatMessageResponse-editForm">
        <input type="hidden" name="id" value="{{d.params.data.model.id || '' }}" />

        <div class="layui-form-item">
            <label for="messageRule" class="layui-form-label ">消息类型</label>
            <div class="layui-input-block">

                <select name="messageRule" lay-filter="messageRuleFilter" lay-verType="tips" lay-verify="required|number" lay-reqText="请选择消息类型">
                    {{#  layui.each(d.params.data.weChatRequestRuleEnum, function(index, item){ }}
                    <option value="{{item.value}}" {{d.params.data.model.messageRule==item.value ? 'selected="selected"':''}}>{{item.description}}</option>
                    {{#  }); }}
                </select>
            </div>
        </div>

        <div class="layui-form-item" id="categoryBox">
            <label for="category" class="layui-form-label ">类型</label>
            <div class="layui-input-block">
                <select name="category" id="category" lay-filter="categoryFilter">
                </select>
            </div>
        </div>

        <div class="layui-form-item" id="matchKeyBox">
            <label for="matchKey" class="layui-form-label">匹配字符</label>
            <div class="layui-input-block">
                <input name="matchKey" lay-verType="tips" class="layui-input" placeholder="请输入匹配字符" lay-reqText="请输入匹配字符" value="{{d.params.data.model.matchKey || '' }}" />
            </div>
        </div>

        <div class="layui-form-item layui-form-text" id="textContentBox">
            <label for="textContent" class="layui-form-label">文本回复内容</label>
            <div class="layui-input-block">
                <input name="textContent" lay-verType="tips" class="layui-input" placeholder="请输入文本回复内容" lay-reqText="请输入文本回复内容" value="{{d.params.data.model.textContent || '' }}" />
            </div>
        </div>

        <div id="ImgBox">

            <div class="layui-form-item layui-form-text">
                <label for="imgTextTitle" class="layui-form-label ">图片回复标题</label>
                <div class="layui-input-block">
                    <input name="imgTextTitle" value="{{ d.params.data.model.messageRule == 3 || d.params.data.model.category == 2 ?  d.params.data.model.textContent : ''}}" class="layui-input" placeholder="请输入图片回复标题" lay-reqText="请输入图片回复标题" />
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label for="imgTextContext" class="layui-form-label">图片回复内容</label>
                <div class="layui-input-block">
                    <input name="imgTextContext" lay-verType="tips" class="layui-input" placeholder="请输入图片回复内容" lay-reqText="请输入图片回复内容" value="{{d.params.data.model.imgTextContext || '' }}" />
                </div>
            </div>

            <div class="layui-form-item">
                <label for="imgTextUrl" class="layui-form-label">图片回复图片地址</label>
                <div class="layui-input-inline layui-inline-5">
                    <input name="imgTextUrl" id="thumbnailInput" lay-verType="tips" class="layui-input" placeholder="请输入图片回复图片地址" lay-reqText="请输入图片回复图片地址" value="{{d.params.data.model.imgTextUrl || '' }}" />
                </div>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="upBtnthumbnail" lay-active="doCropperImg">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="viewImgBoxthumbnail" src="{{ d.params.data.model.imgTextUrl || layui.setter.noImagePicUrl }}">
                            <p id="viewTextBoxthumbnail"></p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="imgTextLink" class="layui-form-label">图片回复超链接</label>
                <div class="layui-input-block">
                    <input name="imgTextLink" lay-verType="tips" class="layui-input" placeholder="请输入图片回复超链接" lay-reqText="请输入图片回复超链接" value="{{d.params.data.model.imgTextLink || '' }}" />
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="enable" class="layui-form-label">是否启用</label>
            <div class="layui-input-inline layui-inline-2">
                <input type="checkbox" lay-filter="switch" name="enable" {{ d.params.data.model.enable ? 'checked' : '' }} lay-skin="switch" lay-text="开启|关闭">
            </div>
            <label for="isDefault" class="layui-form-label">是否默认</label>
            <div class="layui-input-inline layui-inline-2">
                <input type="checkbox" lay-filter="switch" name="isDefault" {{ d.params.data.model.isDefault ? 'checked' : '' }} lay-skin="switch" lay-text="开启|关闭">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="remark" class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" lay-verType="tips" class="layui-input" placeholder="请输入备注" lay-reqText="请输入备注" value="{{d.params.data.model.remark || '' }}" />
            </div>
        </div>

        <div class="layui-form-item">
            <label for="Sort" class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="number" lay-affix="number" min="0" max="999999" name="sort" lay-verType="tips" lay-verify="required|number" class="layui-input" value="{{d.params.data.model.sort || '' }}" placeholder="请输入排序" lay-reqText="请输入排序并为数字" />
            </div>
        </div>


        <div class="layui-form-item text-right core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-WeChatMessageResponse-editForm-submit" id="LAY-app-WeChatMessageResponse-editForm-submit" value="确认编辑">
        </div>
    </div>
</script>
<script>
    var debug = layui.setter.debug;
    layui.data.sendParams = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg'],
            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , cropperImg = layui.cropperImg
                    , coreHelper = layui.coreHelper;

                $('#categoryBox').hide();
                $('#matchKeyBox').show();
                $('#textContentBox').hide();
                $('#ImgBox').hide();


                getmessageRuleFilterData(d.params.data.model.messageRule);

                form.on('select(messageRuleFilter)', function (data) {
                    getmessageRuleFilterData(data.value);
                });


                function getmessageRuleFilterData(value) {
                    console.log(value);
                    if (value == 0) { //默认
                        categoryContent(false)
                        $('#categoryBox').show();
                        $('#matchKeyBox').hide();

                        var categoryId = $('#category').val();
                        if (categoryId == 1) {
                            $('#textContentBox').show();
                            $('#ImgBox').hide();
                        } else if (categoryId == 2) {
                            $('#textContentBox').hide();
                            $('#ImgBox').show();
                        }

                    } else if (value == 1) { //关注回复
                        categoryContent(false)
                        $('#categoryBox').show();
                        $('#matchKeyBox').hide();

                        var categoryId = $('#category').val();
                        if (categoryId == 1) {
                            $('#textContentBox').show();
                            $('#ImgBox').hide();
                        } else if (categoryId == 2) {
                            $('#textContentBox').hide();
                            $('#ImgBox').show();
                        }

                    } else if (value == 2) { //文本回复
                        categoryContent(true)
                        $('#categoryBox').show();
                        $('#matchKeyBox').show();
                        $('#textContentBox').show();
                        $('#ImgBox').hide();

                        $('#category').val(4);

                    } else if (value == 3) { //图片回复
                        categoryContent(true)
                        $('#categoryBox').show();
                        $('#matchKeyBox').show();
                        $('#textContentBox').hide();
                        $('#ImgBox').show();

                        $('#category').val(4);
                    }
                    form.render();
                }


                form.on('select(categoryFilter)', function (data) {
                    if (data.value == 1) { //文本
                        $('#matchKeyBox').hide();
                        $('#textContentBox').show();
                        $('#ImgBox').hide();
                    } else if (data.value == 2) { //图文
                        $('#matchKeyBox').hide();
                        $('#textContentBox').hide();
                        $('#ImgBox').show();
                    } else if (data.value == 3) { //语音
                        //不做处理
                    } else if (data.value == 4) { //相等
                        $('#matchKeyBox').show();
                    } else if (data.value == 5) { //包含
                        $('#matchKeyBox').show();
                    }
                    form.render();
                });


                //是否使用匹配模式
                function categoryContent(bl) {
                    //清空 Select:
                    $("#category").empty();
                    if (bl) {
                        layui.each(d.params.data.weChatReplymatchKeyType, function (index, item) {
                            if (item.Value == d.params.data.model.category) {
                                $("#category").append("<option value='" + item.value + "'  selected='selected'>" + item.Des + "</option>");
                            } else {
                                $("#category").append("<option value='" + item.value + "'>" + item.description + "</option>");
                            }
                        });
                    } else {
                        layui.each(d.params.data.weChatReplyType, function (index, item) {
                            if (item.Value == d.params.data.model.category) {
                                $("#category").append("<option value='" + item.value + "'  selected='selected'>" + item.Des + "</option>");
                            } else {
                                $("#category").append("<option value='" + item.value + "'>" + item.description + "</option>");
                            }
                        });
                    }
                    form.render();
                }

                //项目缩略图图片上传
                $('#upBtnthumbnail').click(function () {
                    cropperImg.cropImg({
                        aspectRatio: 1 / 1,
                        imgSrc: $('#viewImgBoxthumbnail').attr('src'),
                        onCrop: function (data) {
                            var loadIndex = layer.load(2);
                            coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                                if (0 === res.code) {
                                    $('#viewImgBoxthumbnail').attr('src', res.data.fileUrl);
                                    $("#thumbnailInput").val(res.data.fileUrl);
                                    layer.msg(res.msg);
                                    layer.close(loadIndex);
                                } else {
                                    layer.close(loadIndex);
                                    layer.msg(res.msg, { icon: 2, anim: 6 });
                                }
                            });
                        }
                    });
                });

                form.verify({

                    verifymatchKey: [/^.{0,100}$/, '匹配字符最大只允许输入100位字符'],
                    verifyimgTextUrl: [/^.{0,1000}$/, '图片回复图片地址最大只允许输入1000位字符'],
                    verifyimgTextLink: [/^.{0,1000}$/, '图片回复超链接最大只允许输入1000位字符'],
                    verifymeidaUrl: [/^.{0,1000}$/, '语音回复地址最大只允许输入1000位字符'],
                    verifymeidaLink: [/^.{0,1000}$/, '语音回复超链接最大只允许输入1000位字符'],
                    verifyremark: [/^.{0,1000}$/, '备注最大只允许输入1000位字符'],
                    verifycreateBy: [/^.{0,100}$/, '创建来源最大只允许输入100位字符'],
                    verifymodifyBy: [/^.{0,100}$/, '修改来源最大只允许输入100位字符'],
                });
                //重载form
                form.render(null, 'LAY-app-WeChatMessageResponse-editForm');
            })
    };
</script>
